<template>
  <div class="dashboard-container">
    <el-header class="dashboard-header">
      <div class="header-content">
        <div class="header-left">
          <img src="@/assets/logo.svg" alt="Logo" class="logo">
          <h1 class="title">IoT 系统管理平台</h1>
        </div>
        <div class="header-right">
          <div class="user-info">
            <el-icon class="user-icon"><User /></el-icon>
            <span class="nickname">{{ currentUser.nickname || currentUser.username }}</span>
          </div>
          <el-button type="danger" @click="logout" size="large">退出登录</el-button>
        </div>
      </div>
    </el-header>

    <el-main class="dashboard-main">
      <div class="welcome-section">
        <h2 class="welcome-title">欢迎回来，{{ currentUser.nickname || currentUser.username }}！</h2>
        <p class="welcome-text">这是您的专属管理面板，您可以在这里管理系统的所有功能</p>
      </div>

      <div class="module-grid">
        <el-card class="module-card" @click="router.push('/user-list')">
          <div class="module-content">
            <el-icon class="module-icon"><User /></el-icon>
            <h3 class="module-title">用户管理</h3>
            <p class="module-desc">查看和管理所有用户信息</p>
          </div>
        </el-card>

        <el-card class="module-card" @click="router.push('/device-list')">
          <div class="module-content">
            <el-icon class="module-icon"><Monitor /></el-icon>
            <h3 class="module-title">设备管理</h3>
            <p class="module-desc">查看和管理所有设备信息</p>
          </div>
        </el-card>

        <el-card class="module-card" @click="router.push('/profile')">
          <div class="module-content">
            <el-icon class="module-icon"><Setting /></el-icon>
            <h3 class="module-title">个人信息</h3>
            <p class="module-desc">修改您的账号信息</p>
          </div>
        </el-card>

        <el-card class="module-card" @click="router.push('/system-config')">
          <div class="module-content">
            <el-icon class="module-icon"><Tools /></el-icon>
            <h3 class="module-title">系统设置</h3>
            <p class="module-desc">配置系统参数和偏好</p>
          </div>
        </el-card>
      </div>
    </el-main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { User, Monitor, Setting, Tools } from '@element-plus/icons-vue'

const router = useRouter()

// 当前登录用户信息
const currentUser = ref({})

onMounted(() => {
  // 从localStorage获取用户信息
  const userInfo = localStorage.getItem('user')
  if (userInfo) {
    currentUser.value = JSON.parse(userInfo)
  } else {
    // 未登录，跳转到登录页
    router.push('/')
  }
})

// 退出登录
const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('user')
  router.push('/')
}
</script>

<style scoped>
.dashboard-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
}

.dashboard-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 40px;
  height: 80px;
  box-sizing: border-box;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.logo {
  width: 50px;
  height: 50px;
  object-fit: contain;
}

.title {
  color: white;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  color: white;
  font-size: 16px;
}

.user-icon {
  font-size: 24px;
}

.nickname {
  font-weight: 500;
}

.dashboard-main {
  padding: 40px;
  flex: 1;
  overflow: auto;
}

.welcome-section {
  margin-bottom: 40px;
  text-align: center;
}

.welcome-title {
  font-size: 32px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 16px;
}

.welcome-text {
  font-size: 18px;
  color: #606266;
}

.module-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 30px;
  max-width: 1200px;
  margin: 0 auto;
}

.module-card {
  cursor: pointer;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  overflow: hidden;
}

.module-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15);
}

.module-content {
  padding: 40px 20px;
  text-align: center;
}

.module-icon {
  font-size: 64px;
  color: #667eea;
  margin-bottom: 24px;
}

.module-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 12px;
}

.module-desc {
  font-size: 16px;
  color: #606266;
  line-height: 1.6;
}
</style>